using System.Threading.Tasks;
using Admin.NET.Application;
using Admin.NET.Application.Entity;
using Admin.NET.Application.Entity.Business;
using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.EventBus;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace Admin.Net.Plugin.Api;

[ApiDescriptionSettings(new string[] { "业务应用" }, Order = 100)]
public class DeleteMeterService : IDynamicApiController, ITransient, IPrivateDependency
{
	private readonly SqlSugarRepository<ElectricMeter> _rep;

	private readonly SqlSugarRepository<DeleteMeter> _repDel;

	private readonly IEventPublisher _eventPublisher;

	private readonly SqlSugarRepository<RoomBalanceRecord> _balanceRep;

	private readonly RoomService _roomService;

	public DeleteMeterService(SqlSugarRepository<DeleteMeter> repDel, SqlSugarRepository<ElectricMeter> rep, IEventPublisher eventPublisher, SqlSugarRepository<RoomBalanceRecord> balanceRep, RoomService roomService)
	{
		_repDel = repDel;
		_rep = rep;
		_eventPublisher = eventPublisher;
		_balanceRep = balanceRep;
		_roomService = roomService;
	}

	/// <summary>
	/// 分页查询删除电表
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpGet("/electricitydelete/page")]
	public async Task<dynamic> Page([FromQuery] ElectricMeteDeleteInput input)
	{
		ISugarQueryable<DeleteMeter> query = _repDel.Context.Queryable<DeleteMeter>().WhereIF(!string.IsNullOrWhiteSpace(input.Name), (DeleteMeter u) => u.Name.Contains(input.Name.Trim())).WhereIF(!string.IsNullOrWhiteSpace(input.Sn), (DeleteMeter u) => u.Sn.Contains(input.Sn.Trim()));
		ISugarQueryable<MeterOutPut> info = (from u in query
			select new MeterOutPut
			{
				SignalStatus = u.SignalStatus,
				Sn = u.Sn,
				CombinedActiveSharpElectricEnergy = u.CombinedActiveSharpElectricEnergy,
				GateStatus = u.GateStatus,
				Addr = u.Addr,
				CollectorId = u.CollectorId,
				CombinedActiveFlatElectricEnergy = u.CombinedActiveFlatElectricEnergy,
				CombinedActivePeakElectricEnergy = u.CombinedActivePeakElectricEnergy,
				CombinedActiveTotalElectricEnergy = u.CombinedActiveTotalElectricEnergy,
				CombinedActiveValleyElectricEnergy = u.CombinedActiveValleyElectricEnergy,
				TenantId = u.TenantId,
				CreateTime = u.CreateTime,
				CreateUserId = u.CreateUserId,
				Id = u.Id,
				IsDelete = u.IsDelete,
				Multiplier = u.Multiplier,
				Name = u.Name,
				PriceRuleId = u.PriceRuleId,
				PriceName = from s4 in SqlFunc.Subqueryable<ElectricityPriceRule>()
					where (long?)s4.Id == u.PriceRuleId
					select s4.Name,
				ProtocolType = u.ProtocolType,
				RoomId = u.RoomId,
				RoomName = from s3 in SqlFunc.Subqueryable<Room>()
					where (long?)s3.Id == u.RoomId
					select s3.Name,
				BuildingId = u.BuildingId,
				BuildingName = from s6 in SqlFunc.Subqueryable<Building>()
					where (long?)s6.Id == u.BuildingId
					select s6.Name,
				UpdateTime = u.UpdateTime,
				UpdateUserId = u.UpdateUserId,
				IssuedStatus = u.IssuedStatus,
				LowAddr = u.LowAddr,
				LabelId = u.LabelId,
				IsPublicMeter = u.IsPublicMeter,
				LabelName = from s5 in SqlFunc.Subqueryable<LabelManagement>()
					where (long?)s5.Id == u.LabelId
					select s5.Name,
				ElectricityProtectionBegin = u.ElectricityProtectionBegin,
				ElectricityProtectionEnd = u.ElectricityProtectionEnd,
				IsElectricityProtection = u.IsElectricityProtection,
				LatestMeterReadingTime = u.LatestMeterReadingTime,
				Latitude = u.Latitude,
				Longitude = u.Longitude,
				OwnerControl = u.OwnerControl,
				IsSubsidy = u.IsSubsidy,
				SubsidyFee = u.SubsidyFee,
				NowSubsidyFee = u.NowSubsidyFee,
				ReservationSubsidyFee = u.ReservationSubsidyFee,
				SubsidyTime = u.SubsidyTime,
				ReservationSubsidyTime = u.ReservationSubsidyTime,
				BaseCombinedActiveFlatElectricEnergy = u.BaseCombinedActiveFlatElectricEnergy,
				BaseCombinedActivePeakElectricEnergy = u.BaseCombinedActivePeakElectricEnergy,
				BaseCombinedActiveSharpElectricEnergy = u.BaseCombinedActiveSharpElectricEnergy,
				BaseCombinedActiveTotalElectricEnergy = u.BaseCombinedActiveTotalElectricEnergy,
				BaseCombinedActiveValleyElectricEnergy = u.BaseCombinedActiveValleyElectricEnergy,
				BaseCombinedActiveValleyDeepElectricEnergy = u.BaseCombinedActiveValleyDeepElectricEnergy,
				ElectricityLossPercentage = u.ElectricityLossPercentage,
				RSSI = u.RSSI,
				LastOnlineTime = u.LastOnlineTime
			} into u
			orderby u.CreateTime descending
			select u).OrderBuilder(input);
		return await info.ToPagedListAsync(input.Page, input.PageSize);
	}

	/// <summary>
	/// 恢复电表
	/// </summary>
	/// <param name="input"></param>
	/// <returns></returns>
	[HttpPost("/electricitydelete/restore")]
	public async Task RestoreMeter([FromBody] RestoreMeterInput input)
	{
		throw Oops.Bah("暂不支持恢复功能，请重新添加");
	}
}
